package Frame;

import Function.FCPlotLibrary;
import VWorldGameStudio.FCMap;
import VWorldGameStudio.FCMapAndMsEntity;
import VWorldGameStudio.FCPlot;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

/**
 * 编辑或新建剧情。
 */
public class FCEditMonsterRelateMap extends JDialog {
	FCMapAndMsEntity mapAndMsEntity = null;
	public FCEditMonsterRelateMap(boolean isnew, FCMap map, MapEditJpanel jpanel) {
		setTitle((isnew) ? "遇怪添加" : "遇怪编辑");
		setModal(true);
		setBounds(0, 0, 600, 500);
		setLayout(null);
		setLocationRelativeTo(null);
		setResizable(false);
		mapAndMsEntity = new FCMapAndMsEntity();
		JLabel x1 = new JLabel("起始坐标X");
		x1.setBounds(10, 10, 60, 20);
		this.add(x1);
		JTextField x1e = new JTextField();
		x1e.setBounds(70, 10, 60, 20);
		this.add(x1e);

		JLabel y1 = new JLabel("起始坐标Y");
		y1.setBounds(150, 10, 60, 20);
		this.add(y1);
		JTextField y1e = new JTextField();
		y1e.setBounds(210, 10, 60, 20);
		this.add(y1e);

		JLabel select = new JLabel("区域编辑");
		select.setBounds(280, 10, 80, 20);
		if(!isnew) {
			this.add(select);
		}
		JComboBox<String> selecte = new JComboBox<>();
		selecte.setBounds(340, 10, 240, 20);
		selecte.addItem("null");
		for(FCMapAndMsEntity me: map.getMapAndMsEntities()){
			selecte.addItem(me.toString());
		}
		if(!isnew) {
			this.add(selecte);
		}


		JLabel x2 = new JLabel("结束坐标X");
		x2.setBounds(10, 40, 60, 20);
		this.add(x2);
		JTextField x2e = new JTextField();
		x2e.setBounds(70, 40, 60, 20);
		this.add(x2e);


		JLabel y2 = new JLabel("结束坐标Y");
		y2.setBounds(150, 40, 60, 20);
		this.add(y2);
		JTextField y2e = new JTextField();
		y2e.setBounds(210, 40, 60, 20);
		this.add(y2e);

		JLabel level = new JLabel("区域等级");
		level.setBounds(280, 40, 60, 20);
		this.add(level);
		JComboBox<String> levele = new JComboBox<>();
		levele.setBounds(340, 40, 60, 20);
		for(int i = 1; i<4;i++){
			levele.addItem(String.valueOf(i));
		}
		this.add(levele);

		JLabel mid = new JLabel("选择怪物");
		mid.setBounds(10, 70, 60, 20);
		this.add(mid);

		JComboBox<String> mide = new JComboBox<>();
		mide.setBounds(70, 70, 200, 20);
		mide.addItem("null");
		for(String id: FCGMaster.monsterLibrary.monstername){
			mide.addItem(id);
		}
		this.add(mide);

		JLabel mt = new JLabel("遇敌几率");
		mt.setBounds(280, 70, 60, 20);
		this.add(mt);
		JComboBox<String> mte = new JComboBox<>();
		mte.setBounds(340, 70, 60, 20);
		for(int i = 1; i<11;i++){
			mte.addItem(String.valueOf(i));
		}
		this.add(mte);

		JButton add = new JButton("添加");
		add.setBounds(410, 70, 80, 20);
		this.add(add);

		JPanel p = new JPanel() {
			@Override
			protected void paintComponent(Graphics g) {
				g.setColor(Color.white);
				g.fillRect(0, 0, 500, 300);
				g.setColor(Color.black);
				for (int i = 0; i < mapAndMsEntity.getMonsterIds().size(); i++) {
					FCMapAndMsEntity.MS ms = mapAndMsEntity.getMonsterIds().get(i);
					g.drawString(ms.toString(), 5, i * 20 + 17);
					g.drawLine(0, i * 20 + 20, 500, i * 20 + 20);
					g.setColor(Color.white);
					g.fillRect(480, i * 20 + 1, 20, 18);
					g.setColor(Color.black);
					g.drawLine(484, i * 20 + 10, 493, i * 20 + 10);
				}
			}
		};
		p.setBounds(10, 95, 500, 300);
		p.setBorder(BorderFactory.createLineBorder(Color.black));
		this.add(p);
		add.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(!((String)mide.getSelectedItem()).equals("null")){
					FCMapAndMsEntity.MS ms = new FCMapAndMsEntity.MS((String)mide.getSelectedItem(), mte.getSelectedIndex()+1);
					mapAndMsEntity.addMS(ms);
					p.updateUI();
				}
			}
		});

		p.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getX() > 480) {
					int a = e.getY() / 20;
					if(a < mapAndMsEntity.getMonsterIds().size()) {
						mapAndMsEntity.getMonsterIds().remove(a);
					}
					p.updateUI();
				}
			}

			@Override
			public void mousePressed(MouseEvent e) {

			}

			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {

			}

			@Override
			public void mouseExited(MouseEvent e) {

			}
		});
		selecte.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(selecte.getSelectedIndex() != 0){
					mapAndMsEntity = map.getMapAndMsEntities().get(selecte.getSelectedIndex()-1);
					x1e.setText(String.valueOf(mapAndMsEntity.startXY[0]));
					y1e.setText(String.valueOf(mapAndMsEntity.startXY[1]));
					x2e.setText(String.valueOf(mapAndMsEntity.endXY[0]));
					y2e.setText(String.valueOf(mapAndMsEntity.endXY[1]));
					levele.setSelectedItem(String.valueOf(mapAndMsEntity.getLevel()));
					x1e.updateUI();
					y1e.updateUI();
					x2e.updateUI();
					y2e.updateUI();
					levele.updateUI();
					p.updateUI();
				}
			}
		});

		JButton ok = new JButton("保存");
		ok.setBounds(10, 430, 575, 20);
		this.add(ok);
		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(x1e.getText() != null && y1e.getText() != null && x2e.getText() != null && y2e.getText() != null){
					mapAndMsEntity.setLevel(levele.getSelectedIndex()+1);
					mapAndMsEntity.startXY[0] = Integer.parseInt(x1e.getText());
					mapAndMsEntity.startXY[1] = Integer.parseInt(y1e.getText());
					mapAndMsEntity.endXY[0] = Integer.parseInt(x2e.getText());
					mapAndMsEntity.endXY[1] = Integer.parseInt(y2e.getText());
					map.addMapAndMsEntities(mapAndMsEntity);
					jpanel.setNotSave();
					new FCTip("怪物遇怪编辑","保存成功");
					dispose();
				}else {
					new FCTip("怪物遇怪编辑","未能保存");
					dispose();
				}
			}
		});

		setVisible(true);
	}
}
